import React from 'react';
import {Button, Input, Form, Switch, Modal, message, InputNumber, Radio} from 'antd';
import _ from 'lodash';
const RadioGroup = Radio.Group;
const RadioButton = Radio.Button;
const FormItem = Form.Item;
import {fetchArgs, FetchURL, updateData, changePage, getAccount, _validate, check_rules} from '../store/actions';

class FormSpecialistProduct extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            visible: this.props.visible || false,
            title: this.props.title || '添加专家产品',
            targetData: this.props.targetData || {}
        };
        this.onOk = this.onOk.bind(this);
        this.onCancel = this.onCancel.bind(this);
        this.resetFields = this.resetFields.bind(this);
    };

    static contextTypes = {
        router: React.PropTypes.object
    };

    resetFields() {
        this.props.form.resetFields();
    };

    onOk(e) {
        const _this = this;
        _validate(e, _this, function (data) {
            //specialistId专家id
            let _SpecialistId = '';
            const _user = getAccount();
            if (_user) {
                _SpecialistId = _user.user.specialist_id;
            }
            _SpecialistId = _this.props.SpecialistId || _SpecialistId;
            let _args = Object.assign({}, fetchArgs);
            _args.URL = FetchURL.SPRODUCT;
            _args.IsAuth = true;
            _args.SendData = {
                specialist_id: _SpecialistId,
                product_id: _this.props.id,
                price: data.price,
                description: data.description,
                status: (typeof data.status === 'boolean') ? (data.status ? 1 : 0) : (data.status === null ? 0 : data.status)
        }
            _args.Succ = function (res) {
                _this.onCancel();
                message.success('修改专家产品成功', 1)
                _this.setState({
                    visible: false,
                    id: null,
                    title: '',
                    targetData: null
                }, function () {
                    _this.props.reFetch()
                });
            };
            _args.Fail = function () {
                message.error('修改专家产品失败', 1);
            }
            _this.props.dispatch(updateData(_args));
        });
    };

    onCancel() {
        this.props.onCancel();
        this.resetFields();
    };

    setInitialValue(data) {
        const {setFieldsValue} = this.props.form;
        setFieldsValue(data);
    };

    componentWillMount() {
        const _this = this;
        this.props.dispatch(check_rules(function (rules) {
            _this.setState({
                rules: rules.content
            })
        }));
    };

    componentWillReceiveProps(nextProp) {
        this.setState({
            visible: nextProp.visible,
            title: nextProp.title,
            targetData: nextProp.targetData
        });
    };

    componentDidMount() {
        this.setInitialValue(this.state.targetData);
    };

    render() {
        const {getFieldProps} = this.props.form;
        const formItemLayout = {
            labelCol: {span: 6},
            wrapperCol: {span: 16}
        };
        const name = getFieldProps('name', {
            rules: [
                {required: true, message: '请输入名称'}
            ],
            initialValue: this.state.targetData && this.state.targetData.name
        });
        const code = getFieldProps('code', {
            rules: [
                {required: false, message: '请输入编码'}
            ],
            initialValue: this.state.targetData && this.state.targetData.code
        });
        const type = getFieldProps('type', {
            rules: [
                {required: true, message: '请请设置类型'}
            ],
            initialValue: this.state.targetData && this.state.targetData.type
        });
        const duration = getFieldProps('duration', {
            rules: [
                {required: false, type: 'number', message: '请输入时长(分钟)'}
            ],
            initialValue: this.state.targetData && this.state.targetData.duration
        });
        const price = getFieldProps('price', {
            rules: [
                {required: false, type: 'number', message: '请输入价格(元)'}
            ],
            initialValue: this.state.targetData && this.state.targetData.price
        });
        const description = getFieldProps('description', {
            rules: [
                {required: false, message: '请输入描述'}
            ],
            initialValue: this.state.targetData && this.state.targetData.description
        });
        let _status = this.state.targetData ? (this.state.targetData.status == 1 ? true : false) : true;
        const status = getFieldProps('status', {
            rules: [
                {required: false, type: 'boolean', message: '请选择状态'}
            ],
            initialValue: _status
        });
        console.log('type', type);
        return (
            <Modal
                visible={this.state.visible}
                title={this.state.title}
                confirmLoading={this.state.confirmLoading}
                onOk={this.onOk}
                onCancel={this.onCancel}
            >
                <Form horizontal form={this.props.form}>
                    <FormItem {...formItemLayout} label="名称"><Input disabled={!!this.props.id} {...name}
                                                                    placeholder="请输入名称"/></FormItem>
                    <FormItem {...formItemLayout} label="编码"><Input disabled={!!this.props.id} {...code}
                                                                    placeholder="请输入编码"/></FormItem>
                    <FormItem {...formItemLayout} label="类型">
                        <RadioGroup disabled={!!this.props.id} {...type}>
                            <RadioButton value="R">报告</RadioButton>
                            <RadioButton value="G">图文交流/报告</RadioButton>
                            <RadioButton value="V">音视频交流/报告</RadioButton>
                        </RadioGroup>
                    </FormItem>
                    <FormItem {...formItemLayout} label="时长(分钟)"><InputNumber disabled={true} {...duration} placeholder="请输入时长(分钟)"/></FormItem>
                    <FormItem {...formItemLayout} label="价格(元)"><InputNumber min={0} step={0.1} {...price} placeholder="请输入价格(元)"/></FormItem>
                    <FormItem {...formItemLayout} label="描述"><Input type="textarea" {...description} placeholder="请输入描述" autosize={{minRows: 2, maxRows: 6}}/></FormItem>
                    <FormItem {...formItemLayout} label="启用">
                        <Switch {...status} checked={status.value}/>
                    </FormItem>
                </Form>
            </Modal>
        );
    };
}
export default FormSpecialistProduct = Form.create()(FormSpecialistProduct);

